package com.soriya.nestlive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.entity.CategoryEntity;
import com.soriya.nestlive.entity.ChannelEntity;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.param.CategoryListParam;
import com.soriya.nestlive.entity.param.ChannelListParam;
import com.soriya.nestlive.service.ChannelService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/channel")
public class ChannelController {

    private final ChannelService channelService;

    @Autowired
    public ChannelController(ChannelService channelService) {
        this.channelService = channelService;
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size, ChannelListParam param) {
        Page<ChannelEntity> page = new Page<>(current, size);
        PageResult<ChannelEntity> result = channelService.pageQuery(page, param);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result<ChannelEntity> getById(@PathVariable Long id) {
        ChannelEntity data = channelService.getById(id);
        return Result.success(data);
    }

    @PostMapping
    public Result<?> save(@RequestBody ChannelEntity entity) {
        boolean save = channelService.save(entity);
        return Result.update(save);
    }

    @PutMapping
    public Result<?> updateById(@RequestBody ChannelEntity entity) {
        boolean b = channelService.updateById(entity);
        return Result.update(b);
    }

    @DeleteMapping("/{id}")
    public Result<?> removeById(@PathVariable Long id) {
        boolean b = channelService.removeById(id);
        return Result.update(b);
    }

}
